from django import template
from django.template import Node, NodeList, Template, Context, Variable, VariableDoesNotExist
from django.template.loader import render_to_string
from django.template.defaulttags import IfEqualNode

from django_utils.templatetag_helpers import resolve_variable

register = template.Library()

@register.tag(name="toggle_div")
def do_toggle_div(parser,  token):
    try:
        tag,  link_prefix,  link_id,  div_prefix,  div_id,  visible_text,  hidden_text = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError,  "%r tag requires 6 arguments" % token.contents.split()[0]
    return ToggleDiv(link_prefix,  link_id,  div_prefix,  div_id,  visible_text,  hidden_text)

class ToggleDiv(template.Node):
    """
    @link_prefix
    @link_id
    @div_prefix
    @div_id
    @visible_text
    @hidden_text
    """

    def __init__(self,  link_prefix,  link_id,  div_prefix,  div_id,  visible_text,  hidden_text):
        self.link_prefix = link_prefix
        self.link_id = link_id
        self.div_prefix = div_prefix
        self.div_id = div_id
        self.visible_text = visible_text
        self.hidden_text = hidden_text

    def render(self,  context):
        link_prefix = resolve_variable(self.link_prefix, context, self.link_prefix)
        link_id = resolve_variable(self.link_id, context, self.link_prefix)
        div_prefix = resolve_variable(self.div_prefix,  context, self.div_prefix)
        div_id = resolve_variable(self.div_id,  context, self.div_id)
        visible_text = resolve_variable(self.visible_text,  context, self.visible_text)
        hidden_text = resolve_variable(self.hidden_text,  context, self.hidden_text)
        
        full_link_id = "%s%s" % (link_prefix,  link_id)
        full_div_id = "%s%s" % (div_prefix,  div_id)
            
        return render_to_string('django_utils/javascript/toggle_div.html',  
                                {'link_id':full_link_id,  'div_id':full_div_id,  'visible_text':visible_text,  'hidden_text':hidden_text},  context)
